<#import "/jblog.html" as jblog/>
<#import "/jblog-admin.html" as jblogAdmin/>
<div>
	<div>
		<form class="am-form" id="update-article-form">
			<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
			<input type="hidden" name="articleId" value="${article.articleId}">
			<input id="id-article-title" type="text" placeholder="请输入文章标题，不要超过60个字符" name="title" value="${article.title}" />
			<div class="editormd" id="id_editormd">
				<textarea class="editormd-markdown-textarea" name="markdown">${article.markdown}</textarea>
				<textarea class="editormd-html-textarea" name="html"></textarea>
			</div>
			<div class="article-add-line">
				<i class="am-icon-tags am-icon-sm am-text-primary auto-fix-left"/>
				<div class="auto-fix-right">
					<input id="input-sg" type="text" name="tagsStr" value="${article.tagsStr}" />
				</div>
			</div>
			<div class="article-add-line">
				<i class="am-icon-link am-icon-sm am-text-primary auto-fix-left"/>
				<div class="auto-fix-right">
					<input type="text" name="urlName" placeholder="友好的URL名称（选填）" value="${article.urlName}">
				</div>
			</div>
			<div class="article-add-line">
				<span id="id-article-state">
				<input type="hidden" name="state" value="${article.state}">
				<button type="button" class="am-btn am-btn-default am-radius btn-article-state">
					<i class="am-icon-share-alt am-icon-sm" />
				</button>
				<button type="button" class="am-btn am-btn-default am-radius btn-article-state">
					<i class="am-icon-hourglass-start am-icon-sm" />
				</button>
				</span>
				<span id="id-article-menus">
				<input type="hidden" name="menusStr" value="${article.menusStr}">
				<#list blogCommon.leftMenus as menu>					
				<button type="button" class="am-btn am-btn-default am-round btn-select-menu">${menu.title}</button>
				</#list>
				</span>
			</div>
			<input class="am-btn am-btn-success am-btn-xl" type="submit" value="提交"/>
		</form>
	</div>
</div>
<@jblogAdmin.editormdJs jsList=["/amazeui/js/typeahead.min.js","/amazeui/js/amazeui.tagsinput.min.js"]/>
<@jblog.otherJs>
$(function(){
	var substringMatcher = function(strs) {
		return function findMatches(q, cb) {
			var matches, substringRegex;
			matches = [];
			substrRegex = new RegExp(q,"i");
			$.each(strs, function(i, str) {
				if (substrRegex.test(str)){
					matches.push({"title" : str});
				}
			});
			cb(matches);
    	};
	};

	$.get("${rc.contextPath}/tags/admin/list", function(result){
		if(result.resultCode == 0){
			$("#input-sg").tagsinput({
				typeaheadjs : {
		  			name : "tagsHint",
		    		displayKey : "title",
		    		valueKey : "title",
		    		source : substringMatcher(result.data)
				}
			});
		}
	}, "json");

	var menusStr = "${article.menusStr}";
	var showInList = ${article.showInList?string('true','false')};
	var menuInput = $("#id-article-menus :input[type='hidden']");
	if(showInList){	
		$(menuInput).val($(menuInput).val() + '日志列表');
		menusStr = menusStr + '日志列表';
	}
	var menusTitle = menusStr.split(",");
	$(".btn-select-menu").each(function(){
		if(menusTitle.indexOf($(this).html()) > -1){
			$(this).addClass("am-btn-primary");
		}
	});

	var articleState = ${article.state};
	$("#id-article-state button").eq(1 - articleState).addClass("am-btn-primary");
	$(".btn-article-state").bind("click", function(){
		$(".btn-article-state").toggleClass("am-btn-primary");
		$("#id-article-state input[type='hidden']").val($("#id-article-state button").eq(0).hasClass("am-btn-primary") ? 1 : 0);
	});

	$(".btn-select-menu").bind("click",function(){
		$(this).toggleClass("am-btn-primary");
		
		var menuStr = "";
		$("#id-article-menus .am-btn-primary").each(function(){
			menuStr += ($(this).html() + ",");
		});
		$("#id-article-menus :input[type='hidden']").val(menuStr);
	});

	$("#update-article-form :submit").bind("click", function(){
		$.ajax({
			url  : "${rc.contextPath}/article/admin/update", 
			data : $("#update-article-form").serialize(), 
			type : "post",
			success : function(result){
				if(result.resultCode == 0){
					fn_loadFrame("/article/admin/list/page/1");
				}else{
					alert(result.message);
				}
			},
			error : function(){alert("抱歉，来宾帐户无法对系统做出修改！")},
			dataType : "json"
		});
		return false;
	});
});
</@jblog.otherJs>